package controller;

import java.io.IOException;

import application.AtmFrame;
import domain.*;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;

import java.math.BigDecimal;

public class ATM {


    private CardReader cardReader;

    private Card card;

    private AccountService service;

    private Session session;

    private CashDispenser cashDispenser;

    private ReceiptPrinter receiptPrinter;

    private Receipt receipt;


    String[] str = new String[3];

    // 开机
    public void turnOn() {

        cardReader = new CardReaderImpl();
        service = new AccountService();
        cashDispenser = new CashDispenserImpl(BigDecimal.valueOf(10000.00));

    }

    // 插卡
    public ATM() {
        System.out.println("ATM被实例化");
    }

    public boolean pluginCard(String cardId) throws IOException {
        // 实例化card
        card = cardReader.readCard(cardId);
        // 查询该card下是否有账户信息
        Account account = service.getAccountByCard(card);
        if (account == null) {
            return false;
        } else {
            // 跳转到输入密码界面
            return true;
        }
    }

    // 退卡
    public void exitCard() throws IOException {
        // 返回插卡页面
        if (card != null) {
            cardReader = new CardReaderImpl();
            cardReader.exitCard(card);
        }
    }

    // 登录方法
    public boolean LoginAccount(String password) {
        Account account = service.validate(card, password);
        //登录
        if (account != null) {
            //创建会话
            session = Session.getSession(account);
            return true;

        } else {
            return false;

        }
    }

    // 取款交易开始
    public boolean confirmWithdrawl(String a) throws IOException {

        double amount = Double.parseDouble(a);
        //开始交易
        Withdrawl withdrawl = session.beginWithdrawlTransaction();

        if (withdrawl.withdrawl(amount)) {
            session.recordWithdrawl(withdrawl, amount);
            return true;
        }
        return false;

    }

    //打印凭证
    public Receipt receiptPrint() {
        receipt = new Receipt(session.getWithdrawls());
        return receipt;
    }

    public void print() {


    }

    //

    // 按钮设置金额
    public void setAmount() {

    }


}
